package qf.wlh.com.oneprisdent.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import bean.MainBean;
import fragment.detialfragment.SpeakerDetialFragment;
import qf.wlh.com.oneprisdent.PlayVideoActivity;
import qf.wlh.com.oneprisdent.R;

/**
 * Created by zyq on 2016/12/2.
 */

//我是
public class MainViewPagerAdapter extends PagerAdapter {

    private List<MainBean.DataBean> data=new ArrayList<>();
    private Context context;
    private ImageView halo;
    private TextView title;
    private TextView desc;
    private TextView newpc;
    private TextView lecturesTitle;
    private TextView lecturesName;
    private ImageView lecturesCover;
    private Button button;
    private String name;
    private int id;
    private FragmentManager fragmentManager;
    public MainViewPagerAdapter(List<MainBean.DataBean> data, Context context,FragmentManager fragmentManager) {
        this.data = data;
        this.context = context;
        this.fragmentManager=fragmentManager;
    }

    @Override
    public int getCount() {
        return data!=null?data.size():0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof View){
            View view= (View) object;
            container.removeView(view);
        }
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        View view=null;
        view= LayoutInflater.from(context).inflate(R.layout.yanjiang_layout,null);
        initView(view);
        initData(position);
        container.addView(view);
        return view;
    }

    private void initData(int position) {
        Picasso.with(context).load(data.get(position).getBackground()).into(halo);
        title.setText(data.get(position).getTitle());
        desc.setText(data.get(position).getDesc());
        newpc.setText(data.get(position).getNewpc());
        lecturesTitle.setText(data.get(position).getLectures().get(0).getTitle());
        lecturesName.setText(data.get(position).getLectures().get(0).getLecturer().getNickname());
        Picasso.with(context).load(data.get(position).getLectures().get(0).getCover()).into(lecturesCover);
        name=data.get(position).getLectures().get(0).getVideo();
        id=data.get(position).getLectures().get(0).getId();
    }

    private void initView(View view) {
        halo = ((ImageView) view.findViewById(R.id.halo));
        title = ((TextView) view.findViewById(R.id.textview1));
        desc = ((TextView) view.findViewById(R.id.desc));
        newpc = ((TextView) view.findViewById(R.id.textview2));
        lecturesTitle = ((TextView) view.findViewById(R.id.lectures_title));
        lecturesName = ((TextView) view.findViewById(R.id.lectures_name));
        lecturesCover = ((ImageView) view.findViewById(R.id.item_image));
        button = ((Button) view.findViewById(R.id.paly_button));
        initlistener();

    }

    private void initlistener() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, PlayVideoActivity.class);
                intent.putExtra("name",name);
                context.startActivity(intent);

            }
        });
        lecturesCover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();

                bundle.putInt("key",id);

                SpeakerDetialFragment speakerDetialFragment=new SpeakerDetialFragment();
                speakerDetialFragment.setArguments(bundle);
                fragmentManager.beginTransaction()
                        .replace(R.id.container,speakerDetialFragment,"SpeakerDetialFragment")
                        .commit();
            }
        });
    }
}
